package com.zw.web.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zw.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 订单消费者服务
 */
@RestController
public class PayController {

    @Autowired
    private RestTemplate restTemplate ;

    /**
     * 该方法是浏览器来调用
     * @param id
     * @return
     */
    @GetMapping("/pay/user/{id}")
    @HystrixCommand(fallbackMethod = "payFallBack")
    public User getUserById(@PathVariable("id") Long id){
        String url = "http://user-server/user/"+id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    public User payFallBack(@PathVariable("id") Long id){
        return new User(-1l,"没得这个人","你等嘿儿再来嘛");
    }
}